package com.one.rope.mvp.web.common.redis;

import com.one.rope.mvp.basecore.util.JsonUtils;
import com.one.rope.mvp.web.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Weijian.liu
 * @desc Redis单机模式操作工具类
 * @date 2020/02/19
 */
@Slf4j
@Service
public class RedisClientUtil {

  private final static String OK = "OK";

  private static JedisPool jedisPool;

  public void init(CommonConfig commonConfig) {
    try {
      JedisPoolConfig config = new JedisPoolConfig();
      config.setMaxTotal(commonConfig.getMaxTotal());
      config.setMaxIdle(commonConfig.getMaxIdle());
      config.setMaxWaitMillis(commonConfig.getMaxWait());
      config.setTestOnBorrow(commonConfig.isTestOnBorrow());
      jedisPool = new JedisPool(config, commonConfig.getHost(), commonConfig.getPort(),
          commonConfig.getTimeout(), commonConfig.getPassword());
    } catch (Exception e) {
      log.error("redis init error!");
    }
  }

  /**
   * 获取jedis实例
   */
  public synchronized static Jedis getJedis(){
    Jedis jedis = null;
    try {
      //连接池中获取资源
      if (jedisPool != null) {
        jedis = jedisPool.getResource();
      } else {
        return null;
      }
    }catch (RuntimeException e){
      log.error("get redis error:{}", e);
    }finally {
      if(jedis != null ) {
        releaseResource(jedis);
      }
    }
    return jedis;
  }

  /**
   * 释放jedis资源
   */
  public static void releaseResource(final Jedis jedis) {
    if (jedis != null) {
      jedis.close();
    }
  }

  /**
   * 设置值
   */
  public static String set(final String key, final String value) throws Exception{
    Jedis jedis = RedisClientUtil.getJedis();
    return jedis.set(key, value);
  }

  /**
   * 设置值和失效时间
   */
  public static String setex(final String key, final int seconds, final String value) throws Exception{
    Jedis jedis = RedisClientUtil.getJedis();
    return jedis.setex(key, seconds, value);
  }

  /**
   * 获取字符串值
   */
  public static String get(final String key) throws Exception{
    Jedis jedis = RedisClientUtil.getJedis();
    return jedis.get(key);
  }

  /**
   * 删除
   */
  public static Long del(String key) throws Exception{
    Jedis jedis = RedisClientUtil.getJedis();
    return jedis.del(key);
  }

  /**
   * 保存对象，对象序列化为字符串
   */
  public static String setObject(String key, Object obj) throws Exception{
    Jedis jedis = RedisClientUtil.getJedis();
    return jedis.set(key, JsonUtils.objectToFasterxmlJsonString(obj));
  }

  /**
   * 保存对象，对象序列化为字符串，附加过期时间
   */
  public static String setExObject(String key, final int seconds, Object obj) throws Exception{
    Jedis jedis = RedisClientUtil.getJedis();
    return jedis.setex(key, seconds, JsonUtils.objectToFasterxmlJsonString(obj));
  }

  /**
   * 获取对象
   */
  public static <T> T getObject(String key, Class<T> clazz) throws Exception {
    Jedis jedis = RedisClientUtil.getJedis();
    String jsonString = jedis.get(key);
    return JsonUtils.stringToFasterxmlObject(jsonString, clazz);
  }
}
